﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_v01
{
   public partial class CourseList2 : Form
   {
      private int courseID;
      private int instructorID;
      private ArrayList courseDetails;
      private ArrayList lessonDetails;
      private ArrayList lessonQuestions;

      public CourseList2()
      {
         InitializeComponent();
      }

      public int InstructorID
      {
         get
         {
            return instructorID;
         }
         set
         {
            instructorID = value;
         }
      }

      private void CourseList2_Load(object sender, EventArgs e)
      {
         courseComboBox.Items.Clear();

         DBConnection dbConn = new DBConnection();
         courseDetails = dbConn.RetrieveCourses(instructorID);
         foreach (CourseDetail courseDetail in courseDetails)
         {
            string item = courseDetail.SubjectCode + "(" + courseDetail.CourseSession + "/" + courseDetail.TermStart.Year + ")";
            courseComboBox.Items.Add(item);
         }

         if (courseComboBox.Items.Count > 0)
         {
            courseComboBox.Enabled = true;
         }
         else
         {
            courseComboBox.Enabled = false;
         }
      }

      private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
         weekComboBox.Items.Clear();

         CourseDetail temp = (CourseDetail) courseDetails[courseComboBox.SelectedIndex];
         courseID = temp.CourseID;
         DBConnection dbConn = new DBConnection();
         
         //MessageBox.Show("courseID: " + temp.CourseID + " has been selected");
         ArrayList retrievedWeeks = dbConn.RetrieveWeeks(courseID, instructorID);

         int unknownCounter = 0;
         foreach (int week in retrievedWeeks)
         {
            if (week >= 0)
            {
               string item = "Week " + week;
               weekComboBox.Items.Add(item);
            }
            else
            {
               string item = "Unknown " + unknownCounter;
               unknownCounter++;
               weekComboBox.Items.Add(item);
            }
         }

         if (weekComboBox.Items.Count > 0)
         {
            weekComboBox.Enabled = true;
            lessonComboBox.Enabled = false;
            runLessonToolStripButton.Enabled = false;
            editLessonToolStripButton.Enabled = false;
            editCoursePasswordToolStripButton.Enabled = true;
         }
         else
         {
            weekComboBox.Enabled = false;
            lessonComboBox.Enabled = false;
            runLessonToolStripButton.Enabled = false;
            editLessonToolStripButton.Enabled = false;
            editCoursePasswordToolStripButton.Enabled = false;
         }
      }

      private void weekComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
         lessonComboBox.Items.Clear();

         int selectedItem = weekComboBox.SelectedIndex;
         string selectedWeek = weekComboBox.Items[selectedItem].ToString();
         int weekNum = Int32.Parse(selectedWeek.Split(' ')[1]);

         DBConnection dbConn = new DBConnection();
         lessonDetails = dbConn.RetrieveLessons(courseID, instructorID, weekNum);

         monthCalendar1.BoldedDates = null;
         foreach (ClassDetail lesson in lessonDetails)
         {
            string item = lesson.DayOfWeek + " " + lesson.StartTime.ToShortTimeString() + "-" +
               lesson.FinishTime.ToShortTimeString();
            lessonComboBox.Items.Add(item);

            //monthCalendar1.C
            monthCalendar1.AddBoldedDate(lesson.StartTime);
         }

         monthCalendar1.UpdateBoldedDates();
         //monthCalendar1.Refresh();

         if (lessonComboBox.Items.Count > 0)
         {
            lessonComboBox.Enabled = true;
            runLessonToolStripButton.Enabled = false;
            editLessonToolStripButton.Enabled = false;
         }
         else
         {
            lessonComboBox.Enabled = false;
            runLessonToolStripButton.Enabled = false;
            editLessonToolStripButton.Enabled = false;
         }

      }

      private void lessonComboBox_SelectedIndexChanged(object sender, EventArgs e)
      {
         runLessonToolStripButton.Enabled = true;
         editLessonToolStripButton.Enabled = true;
      }

      private int FindLesson(string lessonName)
      {
         int lessonID = 0;

         foreach (ClassDetail lesson in lessonDetails)
         {
            if (lessonName.Contains(lesson.DayOfWeek.ToString()) &&
                lessonName.Contains(lesson.StartTime.ToShortTimeString()) &&
                lessonName.Contains(lesson.FinishTime.ToShortTimeString()))
            {
               lessonID = lesson.ClassID;
               break;
            }
         }

         return lessonID;
      }

      private void CourseList2_FormClosing(object sender, FormClosingEventArgs e)
      {
         MainApp main = (MainApp)this.MdiParent;
         main.ToggleEditCourseButtonEnable();
      }

      private void runLessonButton_Click(object sender, EventArgs e)
      {
         
         
      }

      private void runLessonToolStripButton_Click(object sender, EventArgs e)
      {
         int selectedIndex = lessonComboBox.SelectedIndex;
         //Console.WriteLine(selectedIndex);
         string lessonName = lessonComboBox.Items[selectedIndex].ToString();
         //Console.WriteLine(lessonName);
         int lessonID = FindLesson(lessonName);
         //Console.WriteLine(lessonID);

         MainApp main = (MainApp)this.MdiParent;
         main.RunLesson(lessonID);

         this.Close();
      }

      private void editLessonToolStripButton_Click(object sender, EventArgs e)
      {
         int selectedIndex = lessonComboBox.SelectedIndex;
         string lessonName = lessonComboBox.Items[selectedIndex].ToString();
         int lessonID = FindLesson(lessonName);

         DBConnection dbConn = new DBConnection();
         lessonQuestions = dbConn.RetrieveQuestions(lessonID);

         EditLesson edit = new EditLesson(lessonID, lessonQuestions, lessonName);
         edit.MdiParent = this.MdiParent;
         edit.ListExistingQuestions(lessonQuestions);
         edit.Show();
         MainApp main = (MainApp)this.MdiParent;
         main.ToggleCourseListEnable();
         main.SetEditLesson = edit;
      }

      private void editCoursePasswordToolStripButton_Click(object sender, EventArgs e)
      {
         CourseDetail temp = (CourseDetail)courseDetails[courseComboBox.SelectedIndex];
         //string password = temp.Password;
         EditPassword edit = new EditPassword(temp);
         edit.MdiParent = this.MdiParent;
         edit.Show();

         MainApp main = (MainApp)this.MdiParent;
         main.ToggleCourseListEnable();
      }

      public void UpdateCourseDetail(CourseDetail updated)
      {
         foreach (CourseDetail original in courseDetails)
         {
            if (updated.CourseID == original.CourseID)
            {
               original.CourseSession = updated.CourseSession;
               original.Password = updated.Password;
               original.SubjectCode = updated.SubjectCode;
               original.SubjectTitle = updated.SubjectTitle;
               original.TermFinish = updated.TermFinish;
               original.TermStart = updated.TermStart;
               return;
            }
         }
      }
   }
}
